import React from 'react'
import { Layout,Image} from 'antd';
import Table from '../Table'
import Editor from "../Editor"
import "./tr.css"
import {useSelector,useDispatch,connect} from 'react-redux'
const {Content} = Layout;

const Post = () => {
  const postList = useSelector(state=> state.post.postList)
  const valueList = useSelector(state=> state.value.valueList)
  const dispatch = useDispatch()

  const columns = [
    {
      title: '用户用户名',
      key: 'shareuName',
      dataIndex: 'shareuName',
      valueType: 'text'
    },
    {
      title: '用户头像',
      key: 'shareuImg',
      dataIndex: 'shareuImg',
      valueType: 'avatar',
      hideInSearch:true
    },
    {
      title: '用户评论',
      key: 'shareuTalk',
      dataIndex: 'shareuTalk',
      valueType: 'text',
      ellipsis:true
    },
    {
        title: '分享内容',
        dataIndex: 'shareCon',
        key: 'shareCon',
        ellipsis:true
    },
    {
      title: '分享图片',
      key: 'shareImg',
      dataIndex: 'shareImg',
      ellipsis:true,
      hideInSearch:true,
      render:(text,record)=>{
        if((record.shareImg).search(';') == -1){
          return(
            <Image.PreviewGroup>
              <Image width={30} height ={30} src={record.shareImg} />
            </Image.PreviewGroup>
          )

        }else{
          return (
            <Image.PreviewGroup>
              {
                (record.shareImg).split(";").map((items, index) =>{
                  return <Image width={30} height ={30} src={items} />

                }
                )
              }
            </Image.PreviewGroup>
            
          )
        }
      }
    },
    {
      title: '分享时间',
      key: 'shareTime',
      dataIndex: 'shareTime',
      valueType: 'text'
    },
    
    {
      title: '热词推荐',
      key: 'hotRecom',
      dataIndex: 'hotRecom',
      valueType: 'text',
      ellipsis:true
    },
    {
        title: '操作',
        key: 'option',
        width: 120,
        valueType: 'option',
        render: (text,record,index,action) => [<div onClick={()=>{
          dispatch({type:'get',con:record});console.log(valueList)
        }}><Editor ques={post} url='http://49.233.107.127:2002/post'/></div>
        , <a onClick={async ()=>{
          console.log(record)
          await fetch(`http://49.233.107.127:2002/post/${record.shareID}`,{
          method:'delete',
          headers: {
            'Content-Type': 'application/json;charset=UTF-8'
          }
        })
        dispatch({type:'delete',content:['delete']})
        }}>删除</a>]
      }];
  
    const right = {
      shareImg:{
        show:false,
        order:2
      }
    }
    
    const post = [
      {name:'shareID',label:'分享内容id',tooltip:'最长为24位',placeholder:'输入分享内容id'},
      {name:'shareuName',label:'用户用户名',tooltip:'最长为24位',placeholder:'输入用户用户名'},
      {name:'shareuImg',label:'用户头像',placeholder:'上传图片'},
      {name:'shareImg',label:'分享图片',placeholder:'上传图片'},
      {name:'shareTime',label:'分享时间',tooltip:'最长为24位',placeholder:'输入分享时间'},
      {name:'shareCon',label:'分享内容',placeholder:'输入分享内容'},
      {name:'hotRecom',label:'热词推荐',tooltip:'最长为24位',placeholder:'输入热词推荐'},
      {name:'shareuTalk',label:'用户评论',placeholder:'输入用户评论'}
  ]
    return (
        <Content className="site-layout-background" style={{paddingLeft: 10,margin: 0,minHeight: 280,}}>
          <Table title="帖子管理" columns={columns} right={right} url="http://49.233.107.127:2002/post" ques={post}/>
        </Content>
    )
}

export default connect()(Post)
